<?php
if (file_exists(dirname(__FILE__)."/smarty/Smarty.class.php"))
	include (dirname(__FILE__)."/smarty/Smarty.class.php");
else
	echosys("CANNOT FIND smarty/Smarty.class.php", MSG_ERROR);

function smarty_render($templatefile, $vars=array(), $path=null, $echoit = false) {
	global $conf;
	static $smartys;
	if ($path==null) {
		$usertemplatepath = USER_PATH.substr($conf["userdir"]["tpl"],1);
		$trace = debug_trace(1); $app = ucfirst(strtolower($trace['class']));
		$apptemplatepath = App::getPath($app);

		if (file_exists($usertemplatepath."/".$templatefile)) $path = $usertemplatepath;
		elseif (file_exists($apptemplatepath."/".$templatefile)) $path = $apptemplatepath;
	}
	if (!file_exists($path."/".$templatefile)) return false;	// FILE NOT FOUND
	
	if (!$smartys[md5($path)]) {
		$smartys[md5($path)] = new Smarty();
		$smartys[md5($path)]->template_dir = $path;
		$cpath = USER_PATH.substr($conf["userdir"]["tmp"],1)."/smarty_compiles/".md5($path);
		if (!is_dir($cpath)) if (!mkdir($cpath, 0777, true)) return fatal("Cannot create smarty compiles directory ({$cpath}), check Permissions", MSG_ERROR);
		$smartys[md5($path)]->compile_dir = $cpath;
		if ($conf["smarty"]["debugging"]=="true") $tpl->debugging = true;
		if ($conf["smarty"]["forcerecompile"]=="true") $tpl->forcerecompile = true;
	}
	foreach (Page::$Vars as $var => $val)
		$smartys[md5($path)]->assign($var, $val);
	
	if (is_array($vars)) {
		foreach ($vars as $var => $val)
			$smartys[md5($path)]->assign($var, $val);
	}else{
		$smartys[md5($path)]->assign("item", $vars);
	}
	
	if ($echoit)
		$smartys[md5($path)]->display($templatefile);
	else
		return $smartys[md5($path)]->fetch($templatefile);
}


?>